package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.AttendAudit;
import cn.stylefeng.guns.sys.modular.system.entity.AttendAuditBtyArea;
import cn.stylefeng.guns.sys.modular.system.entity.AttendDismissed;
import cn.stylefeng.guns.sys.modular.system.mapper.AttendDismissedMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.AttendAuditDdSupervisorParam;
import cn.stylefeng.guns.sys.modular.system.model.params.AttendDismissedParam;
import cn.stylefeng.guns.sys.modular.system.model.result.AttendAuditManagerSetResult;
import cn.stylefeng.guns.sys.modular.system.model.result.AttendDismissedResult;
import cn.stylefeng.guns.sys.modular.system.service.AttendAuditBtyArchivesService;
import cn.stylefeng.guns.sys.modular.system.service.AttendAuditBtyAreaService;
import cn.stylefeng.guns.sys.modular.system.service.AttendAuditService;
import cn.stylefeng.guns.sys.modular.system.service.AttendDismissedService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 驳回意见历史表 服务实现类
 * </p>
 *
 * @author wjk
 * @since 2021-09-14
 */
@Service
public class AttendDismissedServiceImpl extends ServiceImpl<AttendDismissedMapper, AttendDismissed> implements AttendDismissedService {



    @Resource
    private AttendAuditService attendAuditService;

    @Resource
    private AttendAuditBtyArchivesService attendAuditBtyArchivesService;

    SimpleDateFormat sdf = new SimpleDateFormat("YYYYMM");
    @Override
    public void add(AttendDismissedParam param){
        AttendDismissed entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(AttendDismissedParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(AttendDismissedParam param){
        AttendDismissed oldEntity = getOldEntity(param);
        AttendDismissed newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public AttendDismissedResult findBySpec(AttendDismissedParam param){
        return null;
    }

    @Override
    public List<AttendDismissedResult> findListBySpec(AttendDismissedParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(AttendDismissedParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 增加驳回员工的驳回信息(批量增加)
     * @param param
     * @param account
     * @return
     */
    @Override
    public int addList(List<AttendAuditManagerSetResult> param, String account) {

        List<AttendAuditManagerSetResult> AttendAuditManagerbuck = new ArrayList<AttendAuditManagerSetResult>();

        for(AttendAuditManagerSetResult attendAuditManagerSetResult : param){
            //确定驳回信息是有数据的
            if(ToolUtil.isNotEmpty(attendAuditManagerSetResult.getDismissedNote())){
                attendAuditManagerSetResult.setAttendAuditId(attendAuditManagerSetResult.getId());//暂存到审核id中，审核id使用aaid

                attendAuditManagerSetResult.setId(UUIDUtils.getId());
                Long id = UUIDUtils.getId();
                attendAuditManagerSetResult.setId(id);

                AttendAuditManagerbuck.add(attendAuditManagerSetResult);
            }
        }
        int size = 0;
        if(AttendAuditManagerbuck.size()>0){
            size = this.baseMapper.addList(AttendAuditManagerbuck);
        }

        return size;
    }


    private Serializable getKey(AttendDismissedParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private AttendDismissed getOldEntity(AttendDismissedParam param) {
        return this.getById(getKey(param));
    }

    private AttendDismissed getEntity(AttendDismissedParam param) {
        AttendDismissed entity = new AttendDismissed();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    /**
     * 删除员工驳回信息
     * @param param
     * @return
     * @Date 2021-10-28
     */
    @Override
    public int deleteAttendDismissedByAccount(AttendAuditDdSupervisorParam param) {
        int size = this.baseMapper.deleteAttendDismissedByAccount(param);
        return size;
    }
    /**
     * （督导审核页面）督导审核，驳回,一个总的驳回；
     * @param param
     * @param account
     * @return
     */
    @Override
    public AttendDismissed addAttendDismissed(AttendDismissed param, String account,String shopName) {
        if(ToolUtil.isEmpty(shopName)){
            return param;
        }
        List<AttendAuditBtyArea> AttendAuditBtyAreaList = this.attendAuditBtyArchivesService.getShopListByDdPhone(account,shopName);
        if(AttendAuditBtyAreaList.size() > 0){
            AttendAuditBtyArea attendAuditBtyArea = AttendAuditBtyAreaList.get(0);
            AttendAudit attendAudit = this.attendAuditService.getAttendAuditByDzPhone(attendAuditBtyArea.getKqDh());
            if(ToolUtil.isNotEmpty(attendAudit)){
                if(ToolUtil.isNotEmpty(param.getDismissedNote())){
                    Long id = UUIDUtils.getId();
                    param.setId(id);
                    param.setAttendAudit(attendAudit.getId());
                    param.setSubmitMonth(attendAudit.getAttendMonth());
                    param.setDdSupervisorId(attendAudit.getDdSupervisorId());
                    param.setDdSupervisorPhone(attendAudit.getDdSupervisorPhone());
                    param.setDdSupervisorName(attendAudit.getDdSupervisorName());
                    param.setDismissedTime(new Date());
                    param.setDismissedState(1);
                    this.save(param);
                }
            }
        }

        return param;
    }
    /**
     * （督导审核页面）督导审核，获取总的驳回,一个总的驳回；
     * @param account
     * @return
     */
    @Override
    public AttendDismissed getAttendDismissed(String account,String shopName) {
        if(ToolUtil.isEmpty(shopName)){
            return new AttendDismissed();
        }
        AttendAudit attendAudit = this.attendAuditService.getAttendAuditByDzPhone(account);
        if(ToolUtil.isNotEmpty(attendAudit)){
            if(ToolUtil.isEmpty(attendAudit.getAttendMonth())){
                //获取相关月份的考勤信息
                Map<String,String> map = attendAuditBtyArchivesService.intoAttendEmployeeMonthListValidationParam(null,null);
                String attendMonth = map.get("attendMonth").replaceAll("-","");
                attendAudit.setAttendMonth(attendMonth);
            }
            List<AttendDismissed>  attendDismissedList = this.baseMapper.getAttendDismissed(account,attendAudit.getAttendMonth(),shopName);
            if(attendDismissedList.size()>0){
                AttendDismissed attendDismissed = attendDismissedList.get(0);
                return attendDismissed;
            }
        }
        return new AttendDismissed();
    }
    /**
     * 修改若有驳回状态的信息，修改驳回信息为历史记录
     * @param aaId
     * @Date 2021-11-23
     */
    @Override
    public int updateDismissedStateByAaId(String aaId){
        return this.baseMapper.updateDismissedStateByAaId(aaId);
    }
}
